
//
//  GoodStoreTableViewCell.m
//  礼物
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "GoodStoreTableViewCell.h"

@implementation GoodStoreTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    //背景图
    self.backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, WIDTH - 10 * 2, (HEIGHT - 113) / 2 - 10 * 2)];
    self.backgroundImageView.backgroundColor = [UIColor whiteColor];
    self.backgroundImageView.layer.masksToBounds = YES;//裁剪掉边框
    self.backgroundImageView.layer.cornerRadius = 5;;//设置圆角度
    self.backgroundImageView.userInteractionEnabled = YES;//用户交互
    [self.contentView addSubview:self.backgroundImageView];
    
    //头像
    self.avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, WIDTH / 5.5, WIDTH / 5.5)];
    self.avatarImageView.layer.masksToBounds = YES;
    self.avatarImageView.layer.cornerRadius = self.avatarImageView.frame.size.width / 2;//圆形半径是(正方形)宽度的一半
    self.avatarImageView.layer.borderWidth = 1;//设置圆角边框的厚度
    self.avatarImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;//设置边框颜色
    [self.backgroundImageView addSubview:self.avatarImageView];
    
    //名字
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 + self.avatarImageView.frame.size.width + 5, self.avatarImageView.frame.origin.y + 5, WIDTH / 2, self.backgroundImageView.frame.size.height / 9)];
    [self.backgroundImageView addSubview:self.nameLabel];
    
    //标语
    self.descLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.frame.origin.x, self.nameLabel.frame.origin.y + self.nameLabel.frame.size.height, WIDTH / 2, self.nameLabel.frame.size.height)];
    [self.backgroundImageView addSubview:self.descLabel];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(self.descLabel.frame.origin.x + self.backgroundImageView.frame.size.width / 2, self.nameLabel.frame.origin.y + 3, self.backgroundImageView.frame.size.width / 4, self.nameLabel.frame.size.height);
    [self.button setTitle:@"品牌主页" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
    [self.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroundImageView addSubview:self.button];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置item大小
    flowLayout.itemSize = CGSizeMake((self.backgroundImageView.frame.size.width - 10) / 2, self.backgroundImageView.frame.size.height / 3.5 * 2);
    //设置item的行间距
    flowLayout.minimumLineSpacing = 5;
    //设置item的上,左,下,右 边界距离
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    //滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.avatarImageView.frame.origin.y + self.avatarImageView.frame.size.height, self.backgroundImageView.frame.size.width, self.backgroundImageView.frame.size.height - self.avatarImageView.frame.origin.y - self.avatarImageView.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.backgroundImageView addSubview:self.collectionView];
    
    //注册cell,标识符必须与cell的一样
    [self.collectionView registerClass:[GoodStoreCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}

-(void)setModel:(GoodStoreModel *)model
{
    _model=model;
    [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:_model.icon_url]];
    self.nameLabel.text = _model.name;
    self.nameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:19];
    self.descLabel.text = _model.desc;
    self.descLabel.textColor = [UIColor lightGrayColor];
    self.descLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    
    [self.collectionView reloadData];//刷新
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.image_urls.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //直接从cell重用池里取
    GoodStoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    NSString *image = [self.model.image_urls objectAtIndex:indexPath.row];
    [cell setCellWithModel:image];//方法传值 (属性传值重写setter方法需要重新调整,别用)
    
    return cell;
}

//点击item触发方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"item = %ld", indexPath.row);
    
}



- (void)buttonAction
{
    //进入详情
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
